package com.orangelife.webservice.service.propfee.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Service;

import com.orangelife.common.AbstractV2TransferService;
import com.orangelife.exception.AppBizException;
import com.orangelife.framework.toolkit.validate.ValidateUtil;
import com.orangelife.service.propfee.IPropFeeLinkPublicService;
import com.orangelife.service.propfee.model.PropFeeLinkPublic;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.service.propfee.IPropFeeLinkPublicWebService;

@Service("PropFeeLinkPublicWebService")
public class PropFeeLinkPublicWebServiceImpl extends AbstractV2TransferService
		implements IPropFeeLinkPublicWebService {

	@Resource(name = "propFeeLinkPublicService")
	private IPropFeeLinkPublicService propFeeLinkPublicService;

	@Override
	public Response getProFeeLinkPublicById(HttpServletRequest request,
			String id) throws Exception, AppBizException {
		/* Id为空的场合 */
		if (StringUtils.isBlank(id)) {
			return this.abnormalResp(100002, "Id不能为空");

		}
		/* Id格式错误的场合 */
		if (!ValidateUtil.checkUUID(id)) {
			logger.error(InfoConst.SYS_INFO_1003, "Id", id);
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "Id", id);
		}

		List<PropFeeLinkPublic> list = propFeeLinkPublicService
				.getProFeeLinkPublicById(id);

		return normalResp(list);

	}

	@Override
	public Response getPropFeeLinkPublicByBean(HttpServletRequest request,
			PropFeeLinkPublic propFeeLinkPublic) throws Exception,
			AppBizException {
		if (propFeeLinkPublic == null) {

			return this.abnormalResp(100002, "参数有误");

		}
		List<PropFeeLinkPublic> list = propFeeLinkPublicService
				.getProFeeLinkPublicByBean(propFeeLinkPublic);
		return this.normalResp(0, 10, 1l, list);
	}

	@Override
	public Response deletePropFeeLinkPublicById(HttpServletRequest request,
			String id) throws Exception, AppBizException {
		/* Id为空的场合 */
		if (StringUtils.isBlank(id)) {
			return this.abnormalResp(100002, "Id不能为空");

		}
		/* Id格式错误的场合 */
		if (!ValidateUtil.checkUUID(id)) {
			logger.error(InfoConst.SYS_INFO_1003, "Id", id);
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "Id", id);
		}

		propFeeLinkPublicService.deleteProFeeLinkPublicById(id);
		return normalResp();
	}

	@Override
	public Response updatePropFeeLinkPublic(HttpServletRequest request,
			PropFeeLinkPublic propFeeLinkPublic) throws Exception,
			AppBizException {
		/* Id为空的场合 */
		if (StringUtils.isBlank(propFeeLinkPublic.getId())) {
			return this.abnormalResp(100002, "Id不能为空");

		}
		/* Id格式错误的场合 */
		if (!ValidateUtil.checkUUID(propFeeLinkPublic.getId())) {
			logger.error(InfoConst.SYS_INFO_1003, "Id",
					propFeeLinkPublic.getId());
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "Id", propFeeLinkPublic.getId());
		}

		if (StringUtils.isBlank(propFeeLinkPublic.getLinkId())) {
			return this.abnormalResp(100002, "关联Id不能为空");

		}

		if (!ValidateUtil.checkUUID(propFeeLinkPublic.getLinkId())) {
			logger.error(InfoConst.SYS_INFO_1003, "关联Id",
					propFeeLinkPublic.getLinkId());
			return this.abnormalResp(CodeConst.SYS_CODE_1003,
					InfoConst.SYS_INFO_1003, "关联Id",
					propFeeLinkPublic.getLinkId());
		}

		propFeeLinkPublicService.updateProFeeLinkPublic(propFeeLinkPublic);
		return normalResp();
	}

	@Override
	public Response insertPropFeeLinkPublicBylist(HttpServletRequest request,
			List<PropFeeLinkPublic> list) throws Exception, AppBizException {
		if (list == null) {

			return this.abnormalResp(100002, "参数错误");
		}

		for (PropFeeLinkPublic propFeeLinkPublic : list) {
			/* Id为空的场合 */
			if (StringUtils.isBlank(propFeeLinkPublic.getId())) {
				return this.abnormalResp(100002, "Id不能为空");

			}
			/* Id格式错误的场合 */

			if (!ValidateUtil.checkUUID(propFeeLinkPublic.getId())) {
				logger.error(InfoConst.SYS_INFO_1003, "Id",
						propFeeLinkPublic.getId());
				return this.abnormalResp(CodeConst.SYS_CODE_1003,
						InfoConst.SYS_INFO_1003, "Id",
						propFeeLinkPublic.getId());
			}

			if (StringUtils.isBlank(propFeeLinkPublic.getLinkId())) {
				return this.abnormalResp(100002, "关联Id不能为空");

			}

			if (!ValidateUtil.checkUUID(propFeeLinkPublic.getLinkId())) {
				logger.error(InfoConst.SYS_INFO_1003, "关联Id",
						propFeeLinkPublic.getLinkId());
				return this.abnormalResp(CodeConst.SYS_CODE_1003,
						InfoConst.SYS_INFO_1003, "关联Id",
						propFeeLinkPublic.getLinkId());
			}

			if (StringUtils.isBlank(propFeeLinkPublic.getLinkType())) {
				return this.abnormalResp(100002, "关联类型不能为空");

			}

			if (StringUtils.isBlank(propFeeLinkPublic.getMeterId())) {
				return this.abnormalResp(100002, "公摊表ID不能为空");

			}
			if (StringUtils.isBlank(propFeeLinkPublic.getItemStat())) {
				return this.abnormalResp(100002, "状态不能为空");
			}
		}

		try {

			propFeeLinkPublicService.insertProFeeLinkPublicByList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return normalResp();
	}

	@Override
	public Response getInfoBymeterid(HttpServletRequest request,
			String meterid, String type) throws Exception, AppBizException {
		if (StringUtils.isBlank(meterid)) {
			return this.abnormalResp(100002, "meterid不能为空");
		}
		if (StringUtils.isBlank(type)) {
			return this.abnormalResp(100002, "type不能为空");
		}
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		if ("1".equals(type)) {
			list = propFeeLinkPublicService
					.selectViewPropfeePropInfoByMeterId(meterid);
		}
		if ("2".equals(type)) {
			list = propFeeLinkPublicService.selectMeterNameByMeterId(meterid);
		}

		
		return this.normalResp(0, 10, 1l, list);
	}
}
